from flask import Flask, send_file, jsonify
from flask_cors import CORS
import os
import dst_control

app = Flask(__name__)
CORS(app, resources={r"/api/*": {"origins": "*"}})

SCRIPT_DIR = os.path.abspath("scripts")
BACKUP_PATH = os.path.join(SCRIPT_DIR, "backups/world_backup.zip")

@app.route("/api/status")
def status():
    return dst_control.status()

@app.route("/api/restart")
def restart():
    return dst_control.restart()

@app.route("/api/stop")
def stop():
    return dst_control.stop()

@app.route("/api/update")
def update():
    return dst_control.update()

@app.route("/api/backup")
def backup():
    return dst_control.backup()

@app.route("/api/start")
def start():
    return dst_control.start()

@app.route("/api/download")
def download():
    if os.path.exists(BACKUP_PATH):
        return send_file(BACKUP_PATH, as_attachment=True)
    return jsonify({"success": False, "output": "备份文件不存在"}), 404

if __name__ == "__main__":
    app.run(host="0.0.0.0", port=5000)
